package com.znsd.zl.sys.emp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.znsd.zl.sys.emp.entity.EmpEntity;
import com.znsd.zl.sys.emp.service.EmpService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName EmpController
 * @Description 描述
 * @author xyx
 * @date 2020-08-20 21:10:50
 * @Version v1.0
 */
@RestController
@RequestMapping("/sys/emp")
public class EmpController extends BaseController<EmpService, EmpEntity, Integer> {


	@Override
	@PostMapping
	public R save(@RequestBody EmpEntity empEntity) {
		System.out.println(empEntity.toString());
		try {
			return service.newSave(empEntity) ? R.ok() : R.error("添加失败");
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@Override
	@DeleteMapping
	public R deleteBatch(@RequestBody List<Integer> ids) {
		service.delete(ids);
		return R.ok();
	}

	@GetMapping("/findByName/{empName}")
	public R findByName(@PathVariable(name="empName") String empName){
		try{
			List<EmpEntity> byName = service.findByName(empName);
			return R.ok().put("list",byName);
		}catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}


	}

	@Override
	@GetMapping("/{pageNum}/{pageSize}")
	public R pageFind(Map<String, Object> params,
	                  @PathVariable(name = "pageNum") Integer pageNum,
	                  @PathVariable(name = "pageSize") Integer pageSize) {
		EmpEntity empEntity=new EmpEntity();
		empEntity.setIsDelete(1);
		PageUtils pageUtils = null;
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.setEntity(empEntity);
		try{
			pageUtils = new PageUtils(service.page(new Page<>(pageNum, pageSize), queryWrapper));
		}catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
		return R.ok().put("page", pageUtils);
	}

	@GetMapping("/{pageNum}/{pageSize}/{empName}")
	public R FindByName(Map<String, Object> params,
	                  @PathVariable(name = "pageNum") Integer pageNum,
	                  @PathVariable(name = "pageSize") Integer pageSize,
					  @PathVariable(name = "empName") String empName) {
		EmpEntity empEntity=new EmpEntity();
		System.out.println("========================"+empName);
		empEntity.setEmpName(empName);
		empEntity.setIsDelete(1);
		PageUtils pageUtils = null;
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.setEntity(empEntity);
		try{
			pageUtils = new PageUtils(service.page(new Page<>(pageNum, pageSize), queryWrapper));
		}catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
		return R.ok().put("page", pageUtils);
	}

	@Override
	@PutMapping
	public R update(@RequestBody EmpEntity empEntity) {
		try {
			return service.update(empEntity) ? R.ok() : R.error("修改失败");
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
	}

}
